package com.oracleoaec.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.oracle.template.JDBCTemplate;
import com.oracle.template.PreparedStatementSetter;
import com.oracle.template.ResultSetHandler;
import com.oracleoaec.dao.AdminDao;
import com.oracleoaec.dao.CarDao;
import com.oracleoaec.dao.UserDao;
import com.oracleoaec.entity.Car;
import com.oracleoaec.entity.Record;
import com.oracleoaec.entity.User;

public class AdminDaoImpl extends JDBCTemplate  implements AdminDao {
	
	private List<Record> listR;
	
	UserDao userDao;
	CarDao carDao;
	public AdminDaoImpl() {
		userDao=new UserDaoImpl();
		carDao=new CarDaoImpl();
	}
	//添加汽车信息
	@Override
	public boolean addCar(final Car car) {
		System.out.println(car.toString());
		System.out.println(car.getStatus()+"namne="+car.getCategory_id()+":"+car.getModel());
		String sql="insert into t_car values(gwm4.nextval,?,?,?,?,?,?,?,?,?,?)";
		if(update(sql,new PreparedStatementSetter() {
			@Override
			public void setValues(PreparedStatement ps) throws SQLException {
				ps.setString(1, car.getColor());
				ps.setString(2,car.getModel());
				ps.setString(3,car.getCarNumber());
				ps.setDouble(4, car.getPrice());
				ps.setDouble(5, car.getRent());
				ps.setString(6,car.getComments());
				ps.setInt(7, car.getStatus());
				ps.setInt(8, car.getUserable());
				ps.setLong(9, car.getBrand_id());
				ps.setLong(10, car.getCategory_id());
			}
		})>0){
			return true;
		}
		return false;
	}
	private String nameS;
	@Override
	public String isExistCarNumber(final String name) {
		query("select car_number from t_car where car_number=?",new PreparedStatementSetter() {
			
			@Override
			public void setValues(PreparedStatement ps) throws SQLException {
				ps.setString(1, name);
			}
		}, new ResultSetHandler() {
			
			@Override
			public void handleRs(ResultSet rs) throws SQLException {
				if(rs.next()){
					nameS=rs.getString("car_number");
					
				}
			}
		});
		return nameS;
	}
	@Override
	public List<Record> queryAllRecord(String type) {//a&10
		listR=new ArrayList<Record>();
		String sql="";
		String t=type.split("&")[0];
		String ID=type.split("&")[1];
		if("1".equals(t)){//查询全部租车信息
			sql="select * from t_record";
		}else if("2".equals(t)){//按照用户编号查看汽
			sql="select * from t_record where user_id="+ID+"";
		}else if("3".equals(t)){//按照汽车ID编号查询租车记录
			sql="select * from t_record where car_id="+ID+"";
		}
		
		query(sql, null, new ResultSetHandler() {
			@Override
			public void handleRs(ResultSet rs) throws SQLException {
				while(rs.next()) {
					//System.out.println("rs==" + rs.getString("start_date"));
					//将租车记录返回给用户观看
					long id=rs.getLong("id");//这个是记录ID
					String startDate=rs.getString("start_date");
					String returnDate=rs.getString("return_date");
					double payment=rs.getDouble("payment");//0.35
					Car car=carDao.queryCarById(rs.getLong("car_id"));
					User user=userDao.queryById(rs.getLong("user_id"));
					listR.add(new Record(id, startDate,returnDate,payment, user, car));
				}
			}
		});
		return listR;
	}
	//用于管理员进行查看所有的汽车信息
	@Override
	public Car queryCarById(long id) {
		return carDao.queryCarById(id);
	}
	
	Car car;
	//管理员修改汽车的租金
	@Override
	public Car updateCarInfo(final long carId,final double price) {
		
		System.out.println("carId=="+carId+"::::::price=="+price);
		
		String sql="update t_car set rent=? where id=?";
		if(update(sql, new PreparedStatementSetter() {
			@Override
			public void setValues(PreparedStatement ps) throws SQLException {
				ps.setDouble(1, price);
				ps.setLong(2, carId);
			}
		})>0){
			car=carDao.queryCarById(carId);
		}
		//然后在返回这个值
		return car;
	}
	
	//管理员修改汽车的上架和下架
	@Override
	public Car updateCarUseable(final long carId,final int useable) {
		
		String sql="update t_car set userable=? where id=?";
		if(update(sql, new PreparedStatementSetter() {
			@Override
			public void setValues(PreparedStatement ps) throws SQLException {
				ps.setDouble(1, useable);
				ps.setLong(2, carId);
			}
		})>0){
			car=carDao.queryCarById(carId);
		}
		//然后在返回这个值
		return car;
	}
}
